package com.sanji.sjmallmodule.order;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sanji.sjmallapp.R;
import com.sanji.sjmallutils.ImageUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zsf on 17-1-7.
 */

public class FirmOrderAdapter extends BaseAdapter {

    private Context mCtx;
    private List<ShopcarChildBean> mChildList = new ArrayList<>();

    private int imgWidth;

    public FirmOrderAdapter(Context context, List<ShopcarChildBean> list) {
        this.mCtx = context;
        this.mChildList = list;

        imgWidth = ImageUtil.setImageWidth(context, 4);
    }

    @Override
    public int getCount() {
        return mChildList.size();
    }

    @Override
    public ShopcarChildBean getItem(int i) {
        return mChildList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            view = LayoutInflater.from(mCtx).inflate(R.layout.item_firm_order, null);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }else{
            holder = (ViewHolder) view.getTag();
        }

        holder.mFirmOrderItemDestImg.setLayoutParams(new RelativeLayout.LayoutParams(imgWidth, imgWidth));

        ShopcarChildBean childBean = getItem(i);

        holder.mFirmOrderItemType.setText(childBean.getGroupTitle());
        Glide.with(mCtx).load(childBean.getImgUrl()).centerCrop().placeholder(R.mipmap.default_3j_img)
                //当加载图片失败时，通过error(Drawable drawable)方法设置加载失败后的图片显示
                .error(R.mipmap.default_3j_img)
                .crossFade().into(holder.mFirmOrderItemDestImg);
        holder.mFirmOrderItemDestTitle.setText(childBean.getGoodsName());
        holder.mFirmOrderItemDestSpec.setText(childBean.getGoodsSpec());
        holder.mFirmOrderItemDestPrice.setText(mCtx.getResources().getString(R.string.price_title)+childBean.getGoodsPrice());
        holder.mFirmOrderItemDestCount.setText("x" + childBean.getGoodsCount());

        List<String> giftList = childBean.getGiftList();
        holder.mFirmOrderItemDestGiftLayout.removeAllViews();
        if (giftList != null && giftList.size() > 0) {
            for (String str : giftList) {
                View giftView = LayoutInflater.from(mCtx).inflate(R.layout.item_gift, null);
                TextView gift = (TextView) giftView.findViewById(R.id.gift_item_tv);
                gift.setText(str);
                holder.mFirmOrderItemDestGiftLayout.addView(giftView);
            }
        }

        if (childBean.getGroupTitle()!=null){
            if (i==0){
                holder.mFirmOrderItemType.setVisibility(View.VISIBLE);
            }else if(childBean.getGroupTitle().equals(getItem(i-1).getGroupTitle())){
                holder.mFirmOrderItemType.setVisibility(View.GONE);
            }else{
                holder.mFirmOrderItemType.setVisibility(View.VISIBLE);
            }
        }


        return view;
    }

    static class ViewHolder {
        @BindView(R.id.firm_order_item_type)
        TextView mFirmOrderItemType;
        @BindView(R.id.firm_order_item_dest_img)
        ImageView mFirmOrderItemDestImg;
        @BindView(R.id.firm_order_item_dest_title)
        TextView mFirmOrderItemDestTitle;
        @BindView(R.id.firm_order_item_dest_spec)
        TextView mFirmOrderItemDestSpec;
        @BindView(R.id.firm_order_item_dest_price)
        TextView mFirmOrderItemDestPrice;
        @BindView(R.id.firm_order_item_dest_count)
        TextView mFirmOrderItemDestCount;
        @BindView(R.id.firm_order_item_dest_gift_layout)
        LinearLayout mFirmOrderItemDestGiftLayout;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
